#! /usr/bin/env bash
set -e

THE_IMAGE_NAME="gallop-cpp-builder:1.0.0"

function docker_rm() {
    local image_name="$1"
    shift
    docker rmi -f "${image_name}"
}

function docker_build() {
    local image_name="$1"
    shift

    local repo_url="$REPO_URL"
    if [[ -z "$repo_url" ]]; then
        repo_url="http://localhost:9000/public/software"
    fi

    docker build \
        --network=host \
        --build-arg SOFTWARE_BASE_URL="${repo_url}" \
        -t "${image_name}" \
        -f Dockerfile \
        .

    local rc=$?

    if [ $rc != 0 ]; then
        echo "docker_build failed!"
        exit 1
    fi	

}


function main() {
    local image_name="${THE_IMAGE_NAME}"
    if [ "$#" -gt 0 ]; then
        image_name="$1"
        shift
    fi

    docker_rm "${image_name}"
    docker_build "${image_name}"
}

main "$@"
